import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller.dart';

/// 内购服务连接状态检查组件
/// 作用：展示内购服务是否可用的状态信息
class ConnectionCheckTile extends GetView<PaymentController> {
  const ConnectionCheckTile({super.key, required this.controller});

  final PaymentController controller;

  @override
  Widget build(BuildContext context) {
    // 加载中状态
    if (controller.state.loading.value) {
      return const Card(child: ListTile(title: Text('Trying to connect...')));
    }

    // 构建连接状态头部
    final Widget storeHeader = ListTile(
      leading: Icon(
        controller.state.isAvailable.value ? Icons.check : Icons.block,
        color: controller.state.isAvailable.value
            ? Colors.green
            : ThemeData.light().colorScheme.error,
      ),
      title: Text('The store is ${controller.state.isAvailable.value ? 'available' : 'unavailable'}.'),
    );

    final List<Widget> children = <Widget>[storeHeader];

    // 内购服务不可用时展示错误信息
    if (!controller.state.isAvailable.value) {
      children.addAll(<Widget>[
        const Divider(),
        ListTile(
          title: Text('Not connected',
              style: TextStyle(color: ThemeData.light().colorScheme.error)),
          subtitle: const Text(
              'Unable to connect to the payments processor. Has this app been configured correctly? See the example README for instructions.'),
        ),
      ]);
    }

    return Card(child: Column(children: children));
  }
}
    